import { getToken, setToken, removeToken } from '@/utils/auth'
import { loginApi, getUserInfoApi } from '@/api/user'

const state = {
  token: getToken(),
  userInfo: {}
  // token: null
}
const mutations = {
  setToken(state, token) {
    state.token = token
    setToken(token)
  },
  removeToken(state) {
    state.token = null
    removeToken()
  },
  setUerInfo(state, data) {
    state.userInfo = data
  }
}

const actions = {
  async login(context, formValue) {
    // 拿到用户手机号密码发起请求获取token
    const token = await loginApi(formValue)
    console.log(token)
    context.commit('setToken', token)
  },
  async getUserInfo(context) {
    const res = await getUserInfoApi()
    context.commit('setUerInfo', res)
  },
  logout(context) {
    context.commit('removeToken')
    context.commit('setUerInfo', {})
  }
}

export default {
  state,
  mutations,
  actions,
  namespaced: true
}
